package com.spa.infrastructure.common;

import org.springframework.stereotype.Component;

/**
 * response工具类.
 *
 * @author liangjun
 */
@Component
public class ResponseHelper<T> {

    /**
     * 成功应答.
     */
    public ResponseMessage<T> success() {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.setCode(CommonCode.SUCCESS.getCode());
        responseMessage.setMessage(CommonCode.SUCCESS.getDescription());
        return responseMessage;
    }

    /**
     * 带有返回体的成功应答.
     */
    public ResponseMessage<T> success(T t) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.setCode(CommonCode.SUCCESS.getCode());
        responseMessage.setMessage(CommonCode.SUCCESS.getDescription());
        responseMessage.setResult(t);
        return responseMessage;
    }

    /**
     * 自定义消息和返回体的成功应答.
     */
    public ResponseMessage<T> success(String message, T t) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.setCode(CommonCode.SUCCESS.getCode());
        responseMessage.setMessage(message);
        responseMessage.setResult(t);
        return responseMessage;
    }

    /**
     * 失败应答.
     */
    public ResponseMessage<T> fail(String code, String message) {
        return createFailMessage(code, message);
    }

    public ResponseMessage<T> fail(CommonCode commonCode) {
        return createFailMessage(commonCode.getCode(), commonCode.getDescription());
    }
    public ResponseMessage<T> fail(String message) {
        return createFailMessage(CommonCode.FAIL.getCode(), message);
    }
    public ResponseMessage<T> fail() {
        return createFailMessage(CommonCode.FAIL.getCode(), CommonCode.FAIL.getDescription());
    }

    /**
     * 失败应答.
     */
    public ResponseMessage<T> createFailMessage(String code, String message) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.setCode(code);
        responseMessage.setMessage(message);
        return responseMessage;
    }
}
